﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using TouchRC.Logic;
using TouchRC.Classes;
using TouchRC.BarManagers;
using System.Drawing;

namespace TouchRC.Custom_Controls
{
    public class NowPlayingButtonPanel : FluidPanel
    {
        #region Private Members

        private List<RemoteControlButton> m_Buttons;

        #endregion

        protected override void InitControl()
        {
            base.InitControl();
            InitPanelControls();
            AddControls();

        }

        protected override void OnPaintBackground(FluidPaintEventArgs e)
        {
            // base.OnPaintBackground(e);
        }

        public Rectangle SetNewBounds
        {
            set
            {
                Bounds = value;
                SetControlPositions(AppSettings.CurrentOrientation);
            }
        }

        public void SetControlPositions(ScreenOrientation screenOrientation)
        {
            if (screenOrientation == ScreenOrientation.Landscape)
            {
                List<RemoteControlButton> sortedButtons = m_Buttons.OrderBy(x => x.LandscapePos).ToList();

                int width = UnscaleX(Bounds.Width);
                int margin = 5;
                int startingX = AppSettings.ButtonDimensions + margin;
                int currX = startingX;
                int currY= 0;
                int imageSize = 40;

                foreach (RemoteControlButton button in sortedButtons)
                {
                    button.Bounds = new Rectangle(button.ScaleX(currX), button.ScaleY(currY),
                    button.ScaleX(imageSize), button.ScaleY(imageSize));
                    currX += imageSize + margin;
                }
            }
            else
            {
                List<RemoteControlButton> sortedButtons = m_Buttons.OrderBy(x => x.PortraitPos).ToList();

                int width = UnscaleX(Bounds.Width);
                int margin = 5;
                int imageSize = 40;
                int startingX = (width - ((4 * imageSize) + (3 * margin))) / 2;
                int secondStartingX = (width - ((3 * imageSize) + (2 * margin))) / 2;
                int currX = startingX;
                int currY = 0;

                int counter = 0;

                foreach (RemoteControlButton button in sortedButtons)
                {
                    if (counter == 4)
                    {
                        currX = secondStartingX;
                        currY += imageSize + margin;
                    }

                    button.Bounds = new Rectangle(button.ScaleX(currX), button.ScaleY(currY),
                    button.ScaleX(imageSize), button.ScaleY(imageSize));
                    currX += imageSize + margin;
                    counter++;
                }
            }
        }

        public void AddControls()
        {
            foreach (RemoteControlButton button in m_Buttons)
            {
                if (!(button.Enum is Skip))
                {
                    Controls.Add(button);
                }
            }
        }

        public void InitPanelControls()
        {
            m_Buttons = new List<RemoteControlButton>();

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Skipback,
                                        ImageFactory.GetMediaImage(MediaIcons.player_start), 1, 1));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Rew,
                                        ImageFactory.GetMediaImage(MediaIcons.player_rew), 2, 2));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Stop,
                                        ImageFactory.GetMediaImage(MediaIcons.player_stop), 3, 5));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Play,
                                        ImageFactory.GetMediaImage(MediaIcons.player_play), 4, 6));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Pause,
                                        ImageFactory.GetMediaImage(MediaIcons.player_pause), 5, 7));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Fwd,
                                        ImageFactory.GetMediaImage(MediaIcons.player_fwd), 6, 3));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Skipfwd,
                                        ImageFactory.GetMediaImage(MediaIcons.player_end), 7, 4));
        }
    }
}
